package cn.edu.xmu.oomall.customer.service;

import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.customer.dao.CustomerDao;
import cn.edu.xmu.oomall.customer.dao.bo.Customer;
import cn.edu.xmu.oomall.customer.mapper.po.CustomerPo;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED)
@RequiredArgsConstructor
public class CustomerService {

    private final static Logger logger = LoggerFactory.getLogger(CustomerService.class);

    private final CustomerDao customerDao;

    public Customer getCustomerById(UserDto user) {

        return customerDao.getCustomerById(user);
    }

    public void updateCustomer(Customer customer, UserDto user) {
        List<CustomerPo> pos = this.customerDao.findByMobile(customer.getMobile());
        if (pos.size() > 0) {
            throw new BusinessException(ReturnNo.CUSTOMER_MOBILEEXIST, String.format(ReturnNo.CUSTOMER_MOBILEEXIST.getMessage(), "手机号被注册", pos.get(0).getId()));
        }
        this.customerDao.save(customer,user);
    }

    public void updatePassword(Customer customer, UserDto user) {
        this.customerDao.save(customer,user);
    }

    public void updateCustomerBanStatus(Long id, UserDto user) {
        Customer customer = customerDao.findById(id);
        customer.updateCustomerBanStatus(user);
    }

    public void updateCustomerReleaseStatus(Long id, UserDto user) {
        Customer customer = customerDao.findById(id);
        customer.updateCustomerReleaseStatus(user);
    }
}
